import { defineStore } from 'pinia'
import { accountLogins, getUserInfoById, getUserMenusByRoleId } from '@/service/login/login'
import type { IAccount } from '@/types'
import { localCache } from '@/utils/cache'
import { LOGIN_TOKENS } from '@/global/constants'
import router from '@/router'
import { mapMenusToRoutes } from '@/utils/map-menus'

interface ILoginState {
  token: string
  userInfo: any
  userMenus: any
}

const useLoginStore = defineStore('login', {
  state: (): ILoginState => ({
    token: '',
    userInfo: {},
    userMenus: []
  }),
  actions: {
    async loginAccountAction(account: IAccount) {
      const loginResult = await accountLogins(account)
      const id = loginResult.data.data.id
      this.token = loginResult.data.data.token

      // 本地缓存
      localCache.setCache(LOGIN_TOKENS, this.token)
      // 获取用户详情
      const userInfoResult = await getUserInfoById(id)
      const userInfo = userInfoResult.data.data
      console.log(userInfoResult)

      this.userInfo = userInfo
      // 获取角色（菜单）
      const userMenusResult = await getUserMenusByRoleId(this.userInfo.role.id)
      const json = {
        code: 0,
        data: [
          {
            id: 50,
            name: '首页s',
            type: 1,
            url: '/main/1home',
            icon: 'el-icon-monitor',
            single: true,
            sort: 1,
            children: [
              {
                id: 83,
                url: '/main/1home/home',
                name: '首页s',
                sort: 108,
                type: 2,
                children: [],
                parentId: 50
              }
            ]
          },
          {
            id: 51,
            name: '系统管理s',
            type: 1,
            url: '/main/6system',
            icon: 'el-icon-setting',
            sort: 2,
            children: [
              {
                id: 52,
                url: '/main/6system/user',
                name: '用户管理',
                sort: 100,
                type: 2,
                children: [
                  {
                    id: 56,
                    url: null,
                    name: '创建用户',
                    sort: null,
                    type: 3,
                    parentId: 52,
                    permission: 'system:users:create'
                  },
                  {
                    id: 57,
                    url: null,
                    name: '删除用户',
                    sort: null,
                    type: 3,
                    parentId: 52,
                    permission: 'system:users:delete'
                  },
                  {
                    id: 58,
                    url: null,
                    name: '修改用户',
                    sort: null,
                    type: 3,
                    parentId: 52,
                    permission: 'system:users:update'
                  },
                  {
                    id: 59,
                    url: null,
                    name: '查询用户',
                    sort: null,
                    type: 3,
                    parentId: 52,
                    permission: 'system:users:query'
                  }
                ],
                parentId: 51
              },
              {
                id: 53,
                url: '/main/6system/department',
                name: '部门管理',
                sort: 101,
                type: 2,
                children: [
                  {
                    id: 17,
                    url: null,
                    name: '创建部门',
                    sort: null,
                    type: 3,
                    parentId: 3,
                    permission: 'system:department:create'
                  },
                  {
                    id: 18,
                    url: null,
                    name: '删除部门',
                    sort: null,
                    type: 3,
                    parentId: 3,
                    permission: 'system:department:delete'
                  },
                  {
                    id: 19,
                    url: null,
                    name: '修改部门',
                    sort: null,
                    type: 3,
                    parentId: 3,
                    permission: 'system:department:update'
                  },
                  {
                    id: 20,
                    url: null,
                    name: '查询部门',
                    sort: null,
                    type: 3,
                    parentId: 3,
                    permission: 'system:department:query'
                  }
                ],
                parentId: 1
              },
              {
                id: 54,
                url: '/main/6system/menu',
                name: '菜单管理',
                sort: 103,
                type: 2,
                children: [
                  {
                    id: 21,
                    url: null,
                    name: '创建菜单',
                    sort: null,
                    type: 3,
                    parentId: 4,
                    permission: 'system:menu:create'
                  },
                  {
                    id: 22,
                    url: null,
                    name: '删除菜单',
                    sort: null,
                    type: 3,
                    parentId: 4,
                    permission: 'system:menu:delete'
                  },
                  {
                    id: 23,
                    url: null,
                    name: '修改菜单',
                    sort: null,
                    type: 3,
                    parentId: 4,
                    permission: 'system:menu:update'
                  },
                  {
                    id: 24,
                    url: null,
                    name: '查询菜单',
                    sort: null,
                    type: 3,
                    parentId: 4,
                    permission: 'system:menu:query'
                  }
                ],
                parentId: 1
              },
              {
                id: 55,
                url: '/main/6system/role',
                name: '角色管理',
                sort: 102,
                type: 2,
                children: [
                  {
                    id: 26,
                    url: null,
                    name: '创建角色',
                    sort: null,
                    type: 3,
                    parentId: 25,
                    permission: 'system:role:create'
                  },
                  {
                    id: 27,
                    url: null,
                    name: '删除角色',
                    sort: null,
                    type: 3,
                    parentId: 25,
                    permission: 'system:role:delete'
                  },
                  {
                    id: 28,
                    url: null,
                    name: '修改角色',
                    sort: null,
                    type: 3,
                    parentId: 25,
                    permission: 'system:role:update'
                  },
                  {
                    id: 29,
                    url: null,
                    name: '查询角色',
                    sort: null,
                    type: 3,
                    parentId: 25,
                    permission: 'system:role:query'
                  }
                ],
                parentId: 1
              }
            ]
          },
          {
            id: 60,
            name: '监控管理s',
            type: 1,
            url: '/main/2supervisory',
            icon: 'el-icon-goods',
            sort: 3,
            children: [
              {
                id: 61,
                url: '/main/2supervisory/trajectory',
                name: '轨迹查询',
                sort: 104,
                type: 2,
                children: [],
                parentId: 60
              },
              {
                id: 62,
                url: '/main/2supervisory/video',
                name: '视频查询',
                sort: 105,
                type: 2,
                children: [],
                parentId: 60
              }
            ]
          },
          {
            id: 63,
            name: '上报管理s',
            type: 1,
            url: '/main/5report',
            icon: 'el-icon-chat-line-round',
            sort: 4,
            single: true,
            children: [
              {
                id: 83,
                url: '/main/5report/report',
                name: '上报管理s',
                sort: 108,
                type: 2,
                children: [],
                parentId: 63
              }
            ]
          },
          {
            id: 64,
            name: '任务管理s',
            type: 1,
            url: '/main/3personnel',
            icon: 'el-icon-chat-line-round',
            sort: 5,
            children: [
              {
                id: 65,
                url: '/main/3personnel/distribute',
                name: '任务派发',
                sort: 108,
                type: 2,
                children: [],
                parentId: 64
              },
              {
                id: 66,
                url: '/main/3personnel/examine',
                name: '任务审批',
                sort: 109,
                type: 2,
                children: [],
                parentId: 64
              }
            ]
          },
          {
            id: 67,
            name: '人员管理s',
            type: 1,
            url: '/main/4quest',
            icon: 'el-icon-chat-line-round',
            sort: 6,
            single: true,
            children: [
              {
                id: 84,
                url: '/main/4quest/quest',
                name: '人员管理s',
                sort: 108,
                type: 2,
                children: [],
                parentId: 67
              }
            ]
          },

          // =============
          {
            id: 38,
            name: '系统总览',
            type: 1,
            url: '/main/analysis',
            icon: 'el-icon-monitor',
            sort: 1,
            children: [
              {
                id: 39,
                url: '/main/analysis/overview',
                name: '核心技术',
                sort: 106,
                type: 2,
                children: [],
                parentId: 38
              },
              {
                id: 40,
                url: '/main/analysis/dashboard',
                name: '商品统计',
                sort: 107,
                type: 2,
                children: [],
                parentId: 38
              }
            ]
          },
          {
            id: 1,
            name: '系统管理',
            type: 1,
            url: '/main/system',
            icon: 'el-icon-setting',
            sort: 2,
            children: [
              {
                id: 2,
                url: '/main/system/user',
                name: '用户管理',
                sort: 100,
                type: 2,
                children: [
                  {
                    id: 5,
                    url: null,
                    name: '创建用户',
                    sort: null,
                    type: 3,
                    parentId: 2,
                    permission: 'system:users:create'
                  },
                  {
                    id: 6,
                    url: null,
                    name: '删除用户',
                    sort: null,
                    type: 3,
                    parentId: 2,
                    permission: 'system:users:delete'
                  },
                  {
                    id: 7,
                    url: null,
                    name: '修改用户',
                    sort: null,
                    type: 3,
                    parentId: 2,
                    permission: 'system:users:update'
                  },
                  {
                    id: 8,
                    url: null,
                    name: '查询用户',
                    sort: null,
                    type: 3,
                    parentId: 2,
                    permission: 'system:users:query'
                  }
                ],
                parentId: 1
              },
              {
                id: 3,
                url: '/main/system/department',
                name: '部门管理',
                sort: 101,
                type: 2,
                children: [
                  {
                    id: 17,
                    url: null,
                    name: '创建部门',
                    sort: null,
                    type: 3,
                    parentId: 3,
                    permission: 'system:department:create'
                  },
                  {
                    id: 18,
                    url: null,
                    name: '删除部门',
                    sort: null,
                    type: 3,
                    parentId: 3,
                    permission: 'system:department:delete'
                  },
                  {
                    id: 19,
                    url: null,
                    name: '修改部门',
                    sort: null,
                    type: 3,
                    parentId: 3,
                    permission: 'system:department:update'
                  },
                  {
                    id: 20,
                    url: null,
                    name: '查询部门',
                    sort: null,
                    type: 3,
                    parentId: 3,
                    permission: 'system:department:query'
                  }
                ],
                parentId: 1
              },
              {
                id: 4,
                url: '/main/system/menu',
                name: '菜单管理',
                sort: 103,
                type: 2,
                children: [
                  {
                    id: 21,
                    url: null,
                    name: '创建菜单',
                    sort: null,
                    type: 3,
                    parentId: 4,
                    permission: 'system:menu:create'
                  },
                  {
                    id: 22,
                    url: null,
                    name: '删除菜单',
                    sort: null,
                    type: 3,
                    parentId: 4,
                    permission: 'system:menu:delete'
                  },
                  {
                    id: 23,
                    url: null,
                    name: '修改菜单',
                    sort: null,
                    type: 3,
                    parentId: 4,
                    permission: 'system:menu:update'
                  },
                  {
                    id: 24,
                    url: null,
                    name: '查询菜单',
                    sort: null,
                    type: 3,
                    parentId: 4,
                    permission: 'system:menu:query'
                  }
                ],
                parentId: 1
              },
              {
                id: 25,
                url: '/main/system/role',
                name: '角色管理',
                sort: 102,
                type: 2,
                children: [
                  {
                    id: 26,
                    url: null,
                    name: '创建角色',
                    sort: null,
                    type: 3,
                    parentId: 25,
                    permission: 'system:role:create'
                  },
                  {
                    id: 27,
                    url: null,
                    name: '删除角色',
                    sort: null,
                    type: 3,
                    parentId: 25,
                    permission: 'system:role:delete'
                  },
                  {
                    id: 28,
                    url: null,
                    name: '修改角色',
                    sort: null,
                    type: 3,
                    parentId: 25,
                    permission: 'system:role:update'
                  },
                  {
                    id: 29,
                    url: null,
                    name: '查询角色',
                    sort: null,
                    type: 3,
                    parentId: 25,
                    permission: 'system:role:query'
                  }
                ],
                parentId: 1
              }
            ]
          },
          {
            id: 9,
            name: '商品中心',
            type: 1,
            url: '/main/product',
            icon: 'el-icon-goods',
            sort: 3,
            children: [
              {
                id: 15,
                url: '/main/product/category',
                name: '商品类别',
                sort: 104,
                type: 2,
                children: [
                  {
                    id: 30,
                    url: null,
                    name: '创建类别',
                    sort: null,
                    type: 3,
                    parentId: 15,
                    permission: 'system:category:create'
                  },
                  {
                    id: 31,
                    url: null,
                    name: '删除类别',
                    sort: null,
                    type: 3,
                    parentId: 15,
                    permission: 'system:category:delete'
                  },
                  {
                    id: 32,
                    url: null,
                    name: '修改类别',
                    sort: null,
                    type: 3,
                    parentId: 15,
                    permission: 'system:category:update'
                  },
                  {
                    id: 33,
                    url: null,
                    name: '查询类别',
                    sort: null,
                    type: 3,
                    parentId: 15,
                    permission: 'system:category:query'
                  }
                ],
                parentId: 9
              },
              {
                id: 16,
                url: '/main/product/goods',
                name: '商品信息',
                sort: 105,
                type: 2,
                children: [
                  {
                    id: 34,
                    url: null,
                    name: '创建商品',
                    sort: null,
                    type: 3,
                    parentId: 16,
                    permission: 'system:goods:create'
                  },
                  {
                    id: 35,
                    url: null,
                    name: '删除商品',
                    sort: null,
                    type: 3,
                    parentId: 16,
                    permission: 'system:goods:delete'
                  },
                  {
                    id: 36,
                    url: null,
                    name: '修改商品',
                    sort: null,
                    type: 3,
                    parentId: 16,
                    permission: 'system:goods:update'
                  },
                  {
                    id: 37,
                    url: null,
                    name: '查询商品',
                    sort: null,
                    type: 3,
                    parentId: 16,
                    permission: 'system:goods:query'
                  }
                ],
                parentId: 9
              }
            ]
          },
          {
            id: 41,
            name: '随便聊聊',
            type: 1,
            url: '/main/story',
            icon: 'el-icon-chat-line-round',
            sort: 4,
            children: [
              {
                id: 42,
                url: '/main/story/chat',
                name: '你的故事',
                sort: 108,
                type: 2,
                children: [],
                parentId: 41
              },
              {
                id: 43,
                url: '/main/story/list',
                name: '故事列表',
                sort: 109,
                type: 2,
                children: [],
                parentId: 41
              }
            ]
          }
        ]
      }

      // const userMenus = userMenusResult.data.data
      const userMenus = json.data
      console.log(userMenus, '这个')

      this.userMenus = userMenus
      // 本地缓存
      localCache.setCache('userInfo', userInfo)
      localCache.setCache('userMenus', userMenus)

      // 动态路由
      const routes = mapMenusToRoutes(userMenus)
      console.log(routes)
      routes.forEach((route) => router.addRoute('main', route))

      router.push('/main')
    },
    loadLocalCacheAction() {
      const token = localCache.getCache(LOGIN_TOKENS)
      const userInfo = localCache.getCache('userInfo')
      const userMenus = localCache.getCache('userMenus')
      if (token && userInfo && userMenus) {
        this.token = token
        this.userInfo = userInfo
        this.userMenus = userMenus
        const routes = mapMenusToRoutes(userMenus)
        routes.forEach((route) => router.addRoute('main', route))
      }
    }
  }
})
export default useLoginStore
